/*   
 * Copyright (c) 2010-2020 Founder Ltd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * Founder. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Founder.   
 *   
 */
package com.wujw.auto.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 文件读写操作工具
 * 
 * @author wujw
 * @version 1.0, 2017年11月10日
 */
public class FileUtil {
    /**
     * 创建目录
     * 
     * @param dirName
     * @return
     * @author wujw
     */
    public static boolean createDir(String dirName) {
        File file = new File(dirName);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return true;
            }
        }else {
            return true;
        }
        return false;
    }

    public static boolean writeFileContent(String filePath, String content) {
        boolean isOk = false;
        File file = new File(filePath);
        if (file.exists()) {
            try {
                // 以写入的方式写入文件
                FileWriter fw = new FileWriter(filePath);
                // 内容写入
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(content);
                bw.flush();
                bw.close();
                fw.close();
                isOk = true;
            } catch (Exception e) {

            }
        }
        return isOk;
    }

    /**
     * 创建带内容的文件
     * 
     * @param fileName
     * @param content
     * @return
     * @author wujw
     */
    public static boolean creatFile(String fileName, String content) {
        boolean isOK = false;
        File file = new File(fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
                writeFileContent(fileName, content);
                isOK = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            writeFileContent(fileName, content);
            isOK = true;
        }
        return isOK;
    }

    public static boolean createFileAtPath(String path, String fileName, String content) {
        boolean isOk = false;
        // 如果路径不存在，就创建一个
        if (createDir(path) == true) {
            StringBuffer bf = new StringBuffer(path);
            bf.append(fileName);
            File file = new File(bf.toString());
            if (!file.exists()) {
                // 创建文件并写入内容
                try {
                    file.createNewFile();
                    writeFileContent(bf.toString(), content);
                    isOk = true;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                // 存在的话就直接创建文件写入内容
                return creatFile(path + "/" + fileName, content);
            }
        }
        return isOk;
    }

    public static File createTempFile(String fileName) {
        File file = new File("temp" + fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
                file.deleteOnExit();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }

    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.exists()) {
            if (file.delete()) {
                return true;
            }
        }
        return false;
    }
}
